/******************************************************
 * @file smarthome_page.c
 * @author Destiny 
 * @brief QT智能家居程序
 * @version 0.1
 * @date 2024-07-18
 *****************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

/* Private Includes ----------------------------------------------------------*/
#include "config.h"
#include "page_manager.h"

/* Private typedef -----------------------------------------------------------*/

/* Private Define ------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/**
 * @brief QT智能家居页面逻辑
 * 
 * @param ptParentPageParams 父页面参数
*/
static void SmartHomePageRun(PT_PageParams ptParentPageParams)
{
    pid_t ChildPid;

    if((ChildPid = fork()) < 0)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"fork error!\r\n");
        goto failure;
    }
    /*子进程*/
    if(ChildPid == 0)
    {
        /*运行QT程序*/
        execl("./",QT_PATH,(char *)NULL);
        DBG_PRINTF(DLOG_LVL_INFO,DLOG_TAG,"running QT....");
        goto failure;
    }
    else
    {
        int iStatus;
        /*等待子进程退出*/
        waitpid(ChildPid, &iStatus, 0);
        DBG_PRINTF(DLOG_LVL_INFO,DLOG_TAG,"Child process exited with status: %d\n", iStatus);
    }

failure:
    return;
}
/**
 * @brief  QT智能家居页面结构体
 * 
*/
static T_PageAction g_tSmaetHomePageAction = {
    .name = "smarthome",
    .Run = SmartHomePageRun,
    // .GetInputEvent = ReaderPageGetInputEvent,
    //.Prepare       = ReaderPagePrepare;
};

/* Public Function Prototypes ------------------------------------------------*/

/**
 * @brief  QT智能家居页面注册函数
 * 
 * @return int 成功：0;失败：其他
*/
int SmartHomePageInit(void)
{
    return RegisterPageAction(&g_tSmaetHomePageAction);
}